<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="请购编号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="请购状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择请购状态" clearable>
          <el-option v-for="dict in dict.type.please_buy_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="下单日期" prop="ddDate">
        <el-date-picker clearable v-model="queryParams.ddDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="下单日期">
        <el-date-picker v-model="dateRange" style="display: flex;" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="需求日期" prop="subDate">
        <el-date-picker clearable v-model="queryParams.subDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择需求日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="下单人" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['develop:LvDo:add']">新增请购单</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
      :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <el-table-column sortable label="请购编号" align="center" prop="nos" />
      <el-table-column sortable align="center" label="请购来源" prop="source">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.please_buy_source" :value="scope.row.source" />
        </template>
      </el-table-column>
      <el-table-column sortable label="订单编号" align="center" prop="code" show-overflow-tooltip />
      <el-table-column sortable label="下单人" align="center" prop="userName" />
      <el-table-column sortable label="制单人" align="center" prop="createBy" />
      <el-table-column sortable label="客户名称" align="center" prop="customerName" show-overflow-tooltip />
      <el-table-column sortable label="下单日期" align="center" prop="ddDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ddDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="需求日期" align="center" prop="subDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column sortable label="" align="center" prop="createTime" show-overflow-tooltip/>-->
      <el-table-column sortable label="请购状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.please_buy_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column align="center" label="附件" prop="files">
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.files"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)">查看明细
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-time" @click="handleShowProcess(scope.row)">查看流转
          </el-button>
          <el-button @click="handlePrint(scope.row)" size="mini" type="text" icon="el-icon-printer">打印
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="modifyXdr(scope.row)">修改下单人
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-edit"
            v-if="scope.row.source === '1' && scope.row.status == '9'" @click="handleOtherUpdate(scope.row)">
            修改(其他请购)
          </el-button>
          <!--<el-button size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:LvDo:edit']">修改</el-button>-->
          <el-popconfirm :title="`确认提交请购单？`" @confirm="handleSubmitQgd(scope.row)"
            v-if="scope.row.source === '1' && scope.row.status == '9'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete">提交到采购</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-drawer title="流转记录" :visible.sync="processDrawerShow" :with-header="true" size="20%">
      <process-please-buy-timeline :pleaseBuyId="pleaseBuyId"></process-please-buy-timeline>
    </el-drawer>

    <PleaseBuyForm-list ref="pleaseBuyFormList" @ok="getList"></PleaseBuyForm-list>
    <addqinggou-form ref="addqinggou" @ok="getList"></addqinggou-form>
    <updateqinggou-form ref="pleaseBuyUpdateForm" @ok="getList"></updateqinggou-form>
    <addqinggou-iron-form ref="addqinggouIronForm" @ok="getList"></addqinggou-iron-form>
    <please-buy-detail-print ref="pleaseBuyDetailPrint"></please-buy-detail-print>
    <modifyXdr-form ref="modifyXdrForm" @ok="getList"></modifyXdr-form>

    <process-please-list-module ref="processPleaseListModule"></process-please-list-module>


  </div>
</template>

<script>
import { deleteAction, getAction, getById } from '@/api/manage'
import PleaseBuyFormList from '@/views/develop/pleaseBuy/prjForGoodsIn/PleaseBuyFormList'
import AddqinggouForm from '@/views/develop/pleaseBuy/prjForGoodsIn/AddqinggouForm'
import UpdateqinggouForm from '@/views/develop/pleaseBuy/prjForGoodsIn/UpdateqinggouForm'
import AddqinggouIronForm from "@/views/develop/pleaseBuy/pleaseBuyIron/module/AddqinggouIronForm.vue";
import PleaseBuyDetailPrint from "@/views/develop/pleaseBuy/prjForGoodsIn/PleaseBuyDetailPrint.vue";
import ProcessPleaseListModule from "@/views/develop/process/processPleaseBuy/module/ProcessPleaseListModule.vue";
import ProcessPleaseBuyTimeline from "@/views/develop/process/processPleaseBuy/ProcessPleaseBuyTimeline.vue";
import ModifyXdrForm from "@/views/develop/process/processPleaseBuy/ModifyXdrForm.vue";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";

export default {
  dicts: ['please_buy_status', 'please_buy_source'],
  name: "PleaseBuyList",
  components: {
    FileUploadShow,
    ProcessPleaseBuyTimeline,
    ProcessPleaseListModule,
    PleaseBuyDetailPrint,
    AddqinggouIronForm,
    //LvDoForm,
    PleaseBuyFormList,
    AddqinggouForm,
    UpdateqinggouForm,
    ModifyXdrForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单表格数据
      dataList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        ddDate: null,
        subDate: null,
        type: null,
        ywType: null,
        tax: null,
        customerId: null,
        customerName: null,
        isBuy: null,
        money: null,
        counts: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        userName: null,
        linkman: null,
        linktel: null,
        idcard: null,
        moneycard: null,
        orderByColumn: 'createTime',
        isAsc: 'descending',
      },
      url: {
        list: '/lvDo/lvDo/list',
        remove: '/lvDo/lvDo/',
        export: '/lvDo/lvDo/export',
        submitQgd: '/lvDo/lvDo/submitQgd/',
      },
      tableMaxHeight: window.innerHeight - 320,
      baseUrl: process.env.VUE_APP_BASE_API,
      processDrawerShow: false,
      pleaseBuyId: null,
    };
  },
  created() {
    this.queryParams.type = this.$route.query.type;
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    modifyXdr(row) {
      this.$refs.modifyXdrForm.handleUpdate(row);
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    handleSubmitQgd(row) {
      const ids = row.id || this.ids;
      getById(this.url.submitQgd, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("提交成功！");
      }).catch(() => { });
    },
    handleOtherUpdate(row) {
      this.$refs.pleaseBuyUpdateForm.showDoList(row);
    },
    handleShow(row) {
      this.$refs.pleaseBuyFormList.showDoList(row, 2);
    },

    /** 查看流转信息 */
    handleShowProcess(row) {
      this.processDrawerShow = true;
      this.pleaseBuyId = row.id;
    },

    /** 查询请购单列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange, "ApplyTime");
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.queryParams.status = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addqinggou.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `lvDo_${new Date().getTime()}.xlsx`)
    },

    handlePrint(row) {
      this.$refs.pleaseBuyDetailPrint.showDetail(row);
    },


  }
};
</script>
